package gl.android.biyiqing.fragment;

import gl.android.biyiqing.R;
import gl.android.biyiqing.api.API;
import gl.android.biyiqing.competent.OnLoveAskItemClickListener;
import gl.common.AjaxCallBackProxy;
import gl.common.BaseControl;
import gl.uitls.Tip;

import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class LoveAskDetailFragment extends BaseFragment {

	private String askId;

	// End Of Content View Elements

	private void bindViews() {
	}

	public LoveAskDetailFragment() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		askId = getArguments().getString(OnLoveAskItemClickListener.KEY_ASK_ID);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_lovedetail, container,
				false);
		bindViews();
		initData();
		return rootView;
	}

	private void initData() {
		BaseControl.get(API.URL + "GetAskListDetail?askid=%s",
				new AjaxCallBackProxy() {
					@Override
					public void onSuccess(String t) {
						if (!isValueFragment()) {
							return;
						}
						super.onSuccess(t);

						try {
							final JSONArray jsonArray = new JSONObject(t)
									.getJSONArray("data");
							if (jsonArray == null) {
								Tip.show(NOTFOUND);
								return;
							}

							dataJSONArray = jsonArray;
							final JSONObject jsonObject = jsonArray.getJSONObject(0);
							setText("\t\t"+jsonObject.optString("content"),R.id.textView2);
							setText(jsonObject.optString("title"),R.id.textView1);
							TextView tv = new TextView(getActivity());
							tv.setText(dataJSONArray.toString());
						} catch (Exception e) {
							e.printStackTrace();
							onFailure(e, 0, e.getMessage());
						}
					}

					public void setText(final String string, final int textview1) {
						((TextView) findViewById(textview1)).setText(string);
					}

					@Override
					public void onFailure(Throwable t, int errorNo,
							String strMsg) {
						super.onFailure(t, errorNo, strMsg);
						if (!isValueFragment()) {
							return;
						}
						Tip.show(NOTFOUND);
					}
				}, askId);

	}
}
